<template>
  <div id="rose-chart">
    <div class="rose-chart-title">运行事件模块分布</div>
    <dv-charts :option="option" />
  </div>
</template>

<script>
import _ from "lodash";
export default {
  name: "RoseChart",
  data() {
    return {
      option: {},
    };
  },
  methods: {
    createData() {
      const { randomExtend } = this;
      
      var _datas = [];
      this.axios({
        url: "/alarms_stats/",
        method: "GET",
        responseType: "json",
      }).then((res) => {
        var jsondata = JSON.parse(JSON.stringify(res.data));

        for (let k in jsondata["module_stats"]) {
          let v = jsondata["module_stats"][k];
          _datas.push({
            name: k,
            value: v,
          });
        }
//console.log("rose data:", _datas.length);
        this.option = {
          series: [
            {
              type: "pie",
              radius: "50%",
              roseSort: false,
              data: _datas,
              insideLabel: {
                show: false,
              },
              outsideLabel: {
                formatter: "{name} {percent}%",
                labelLineEndLength: 20,
                style: {
                  fill: "#fff",
                },
                labelLineStyle: {
                  stroke: "#fff",
                },
              },
              roseType: true,
            },
          ],
          color: [
            "#da2f00",
            "#fa3600",
            "#ff4411",
            "#ff724c",
            "#541200",
            "#801b00",
            "#a02200",
            "#5d1400",
            "#b72700",
          ],
        };
      });
    },
    randomExtend(minNum, maxNum) {
      if (arguments.length === 1) {
        return parseInt(Math.random() * minNum + 1, 10);
      } else {
        return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
      }
    },
  },
  mounted() {
    const { createData } = this;
    this.option = {
        series: [
          {
            type: "pie",
            radius: "50%",
            roseSort: false,
            data: [
              {
                name: "name1",
                value: 10,
              },
              {
                name: "name2",
                value: 20,
              },
              {
                name: "name3",
                value: 20,
              },
              {
                name: "name4",
                value: 20,
              },
            ],
            insideLabel: {
              show: false,
            },
            outsideLabel: {
              formatter: "{name} {percent}%",
              labelLineEndLength: 20,
              style: {
                fill: "#fff",
              },
              labelLineStyle: {
                stroke: "#fff",
              },
            },
            roseType: true,
          },
        ],
        color: [
          "#da2f00",
          "#fa3600",
          "#ff4411",
          "#ff724c",
          "#541200",
          "#801b00",
          "#a02200",
          "#5d1400",
          "#b72700",
        ],
      };
    createData();

    setInterval(createData, 30000);
  },
};
</script>

<style lang="less">
#rose-chart {
  width: 30%;
  height: 100%;
  background-color: rgba(6, 30, 93, 0.5);
  border-top: 2px solid rgba(1, 153, 209, 0.5);
  box-sizing: border-box;

  .rose-chart-title {
    height: 50px;
    font-weight: bold;
    text-indent: 20px;
    font-size: 20px;
    display: flex;
    align-items: center;
  }

  .dv-charts-container {
    height: calc(~"100% - 50px");
  }
}
</style>
